package com.umgsai.thread.thread48;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Service {
	private Lock lock = new ReentrantLock();
	public Condition condition = lock.newCondition();
	public void awaitA() {
		try {
			lock.lock();
			Thread.sleep(500);
			System.out.println(Thread.currentThread().getName() + " begin awaitA时间为" + System.currentTimeMillis());
			condition.await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " end awaitA时间为" + System.currentTimeMillis());
			lock.unlock();
		}
	}
	
	public void awaitB() {
		try {
			lock.lock();
			Thread.sleep(500);
			System.out.println(Thread.currentThread().getName() + " begin awaitB时间为" + System.currentTimeMillis());
			condition.await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " end awaitB时间为" + System.currentTimeMillis());
			lock.unlock();
		}
	}
	
	public void signalAll() {
		try {
			lock.lock();
			System.out.println(Thread.currentThread().getName() + " end signalAll时间为" + System.currentTimeMillis());
			condition.signalAll();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
}
